<template>
  <div class="app-container">
    <div class="search-container">
      <div class="button-container">
        <el-button :disabled="true" type="danger"><el-icon>
            <Plus />
          </el-icon>&nbsp;新增</el-button>
        <el-button :disabled="false" type="danger" @click="submitForm(ruleFormRef)"><el-icon>
            <Plus />
          </el-icon>&nbsp;保存</el-button>
        <el-button :disabled="true" type="danger"><el-icon>
            <Plus />
          </el-icon>&nbsp;撤销</el-button>
        <el-button :disabled="true" type="danger"><el-icon>
            <Plus />
          </el-icon>&nbsp;删除</el-button>
      </div>
      <div>
        <el-form ref="dataFormRef" style="max-width: 100% ;margin-top:20px" :model="form" :rules="rules"
          label-width="auto" class="demo-ruleForm" :inline="true" :size="formSize" status-icon>
          <el-form-item label="负责人" prop="buyer">
            <el-select v-model="form.userId" clearable style="width: 150px">
              <el-option label="刘永刚" :value="1" />
              <el-option label="孟盘龙" :value="2" />
              <el-option label="赵秋实" :value="3" />
              <el-option label="牛骞若" :value="4" />
              <el-option label="赵思彤" :value="5" />
              <el-option label="于晋哲" :value="6" />
            </el-select>
          </el-form-item>
          <el-form-item label="分娩日期" required style="width: 300px">
            <el-form-item prop="date1">
              <el-date-picker v-model="form.childbirthDate" type="date" aria-label="Pick a date" format="YYYY-MM-DD"
                value-format="YYYY-MM-DD" placeholder="Pick a date" style="width: 100%" />
            </el-form-item>
          </el-form-item>
          <el-form-item label="单据号" prop="buyer" style="width: 150px">
            -- --
          </el-form-item>
        </el-form>
      </div>

      <div>
        <el-table :data="tableDate" style="width: 100% ; height:350px">
          <el-table-column prop="id" label="序号" align="center" width="180">
            <template #default="scope">
              <span>{{ scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="earTag" label="耳号" align="center" width="180">
            <template #default="scope">
              <el-select v-model="scope.row.earTag" placeholder="请选择耳号" @change="detilLambWean()">
                <el-option :label="item.earTag" :value="item.earTag" v-for="item in tableDatas" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="gestationDays" label="妊娠天数" align="center" width="180">
            <template #default="scope">
              <el-input v-model="scope.row.gestationDays" />
            </template>
          </el-table-column>
          <el-table-column prop="difficultyDelivery" label="分娩难易" align="center" width="180">
            <template #default="scope">
              <el-select v-model="scope.row.difficultyDelivery" placeholder="请选择妊检方式">
                <el-option label="顺产" :value="1" />
                <el-option label="难产" :value="2" />
                <el-option label="助产" :value="3" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="fatSheep" label="肥羊批次" align="center" width="180">
            <template #default="scope">
              <el-select v-model="scope.row.fatSheep" placeholder="请选择肥羊批次">
                <el-option label="乳羊" :value="1" />
                <el-option label="大肥" :value="2" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="motherHood" label="母性" align="center" width="180">
            <template #default="scope">
              <el-select v-model="scope.row.motherHood" placeholder="请选择母性">
                <el-option label="1分" :value="1" />
                <el-option label="2分" :value="2" />
                <el-option label="3分" :value="3" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="lactation" label="泌乳" align="center" width="180">
            <template #default="scope">
              <el-select v-model="scope.row.lactation" placeholder="请选择泌乳">
                <el-option label="1分" :value="1" />
                <el-option label="2分" :value="2" />
                <el-option label="3分" :value="3" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="mastitis" label="乳房炎" align="center" width="180">
            <template #default="scope">
              <el-select v-model="scope.row.mastitis" placeholder="请选择乳房炎">
                <el-option label="1分" :value="1" />
                <el-option label="2分" :value="2" />
                <el-option label="3分" :value="3" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="broodNumber" label="窝号" align="center" width="180">
            <template #default="scope">
              <el-input v-model="scope.row.broodNumber" />
            </template>
          </el-table-column>
          <el-table-column prop="healthyNumber" label="健羔数" align="center" width="180">
            <template #default="scope">
              <el-input v-model="scope.row.healthyNumber" />
            </template>
          </el-table-column>
          <el-table-column prop="lambNumber" label="弱羔数" align="center" width="180">
            <template #default="scope">
              <el-input v-model="scope.row.lambNumber" />
            </template>
          </el-table-column>
          <el-table-column prop="malformationNumber" label="畸形数" align="center" width="180">
            <template #default="scope">
              <el-input v-model="scope.row.malformationNumber" />
            </template>
          </el-table-column>
          <el-table-column prop="stillbirthNumber" label="死胎数" align="center" width="180">
            <template #default="scope">
              <el-input v-model="scope.row.stillbirthNumber" />
            </template>
          </el-table-column>
          <el-table-column prop="mummyNumber" label="木乃伊数" align="center" width="180">
            <template #default="scope">
              <el-input v-model="scope.row.mummyNumber" />
            </template>
          </el-table-column>
          <el-table-column prop="freaksNumber" label="畸胎数" align="center" width="180">
            <template #default="scope">
              <el-input v-model="scope.row.freaksNumber" />
            </template>
          </el-table-column>
          <el-table-column prop="liveNumber" label="活羔数" align="center" width="180">
            <template #default="scope">
              <el-input v-model="scope.row.liveNumber" />
            </template>
          </el-table-column>
          <el-table-column prop="litterWeight" label="窝总重(公斤)" align="center" width="180">
            <template #default="scope">
              <el-input v-model="scope.row.litterWeight" />
            </template>
          </el-table-column>
          <el-table-column prop="sex" label="性别" align="center" width="180">
            <template #default="scope">
              <el-select v-model="scope.row.sex" placeholder="请选择母性">
                <el-option label="母" :value="false" />
                <el-option label="公" :value="true" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="currentBuilding" label="当前栋舍" width="140">
            <template #default="scope">
              <el-select v-model="scope.row.currentBuilding" placeholder="请选择栋舍ID"
                @change="getcolumnIdList(scope.row.currentBuilding)" :disabled="true">
                <el-option :label="item.ridgepoleName" :value="item.ridgepoleId" v-for="item in buildingIdList" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="currentField" label="当前栏位" width="140">
            <template #default="scope">
              <el-select v-model="scope.row.currentField" placeholder="请选择当前栏位" :disabled="true">
                <el-option :label="item.columnName" :value="item.columnManagementId" v-for="item in columnIdList" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="toBuildingManageId" label="转入栋舍" width="140">
            <template #default="scope">
              <el-select v-model="scope.row.toBuildingManageId" placeholder="请选择转入栋舍"
                @change="getcolumnIdList(scope.row.toBuildingManageId)">
                <el-option :label="item.ridgepoleName" :value="item.ridgepoleId" v-for="item in buildingIdList" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="toColumMangementId" label="转入栏位" width="140">
            <template #default="scope">
              <el-select v-model="scope.row.toColumMangementId" placeholder="请选择转入栏位">
                <el-option :label="item.columnName" :value="item.columnManagementId" v-for="item in columnIdList" />
              </el-select>
            </template>
          </el-table-column>
        </el-table>

      </div>
      <div class="count1">
        <el-button link type="danger" @click="Addform()"> + 增行 </el-button>
        <el-button link type="danger" @click="Deleteform()"> - 删行 </el-button>
      </div>
      <div class="count2">
        <el-form ref="ruleFormRef" :model="form" label-width="auto" class="demo-ruleForm" status-icon>
          <el-form-item label="备注">
            <el-input type="textarea" placeholder="请输入备注信息" />
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>

</template>
<script setup lang="ts">
import sheepPregnancy from '@/api/SheepPregnancy/index'
import { sheeppregnancyQuerys } from '@/api/SheepPregnancy/model'
import sheepfile from '@/api/ProductionManagement/index'
import purcure from '@/api/PurchaseMent/index'
import sheepDeliverys from '@/api/Deliverys/index'
import { sheepdeliveryQuerys } from '@/api/Deliverys/model'
import router from '@/router';
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()


//分娩添加表单参数
const form = reactive({
  "childbirthDate": "",
  "userId": null,
})
//分娩添加表格参数
const tableDate = reactive([
  {
    "earTag": "",
    "gestationDays": null,
    "difficultyDelivery": null,
    "childbirthDate": "",
    "fatSheep": null,
    "motherHood": null,
    "lactation": null,
    "mastitis": null,
    "broodNumber": "",
    "broodWeight": null,
    "litterWeight": null,
    "liveLambNum": null,
    "liveLambWeight": null,
    "liveLambAvgWeight": null,
    "dieLambNum": null,
    "dieLambWeight": null,
    "dieLambAvgWeight": null,
    "healthyNumber": null,
    "lambNumber": null,
    "malformationNumber": null,
    "stillbirthNumber": null,
    "mummyNumber": null,
    "freaksNumber": null,
    "liveNumber": null,
    "sex": null,
    "currentBuilding": null,
    "currentField": null,
    "toBuildingManageId": null,
    "toColumMangementId": null,
    "userId": null,
  }
])

//验证
const rules = reactive<FormRules<RuleForm>>({
  name: [
    { required: true, message: 'Please input Activity name', trigger: 'blur' },
    { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
  ],
})



//获取耳号
const sheepPregnancys = reactive<sheeppregnancyQuerys>({
  "startPregnancyDate": "",
  "endPregnancyDate": "",
  "eartag": "",
  "userId": 0,
  "pregnancyResult": 0,
  "parityNum": 0,
  "pageIndex": 1,
  "pageSize": 100
})
//妊检显示方法
const tableDatas: any = ref([])
const getSheepPregnancy = () => {
  sheepPregnancy.getList(sheepPregnancys).then((res: any) => {
    console.log('完整响应数据:', res);
    tableDatas.value = res.data.data
  })
}

//获取栋舍
const forms = reactive<buildingIdQuery>({
  "pageIndex": 1,
  "pageSize": 100,
  "ridgepoleTypeId": 0,
  "ridgepoleName": "",
  "responsiblePerson": ""
})
const buildingIdList: any = ref([])
const getBuildingIdList = () => {
  purcure.getrid(forms).then((res: any) => {
    buildingIdList.value = res.data.data
  })
}

//获取栏位
const columnIdList: any = ref([])
const getcolumnIdList = (id: any) => {
  console.log(id)
  purcure.getcolumn(id).then((res: any) => {
    columnIdList.value = res.data
    console.log(columnIdList.value)
  })
  columnIdList.value = []
}

//反填
const detilLambWean = async () => {
  for (let item of tableDate) {
    try {
      const res = await sheepfile.detilsheepfile(item.earTag);

      item.earTag = res.data.earTag;
      getBuildingIdList();
      item.currentBuilding = res.data.buildingId;

      getcolumnIdList(item.currentBuilding);
      item.currentField = res.data.columnId;
    } catch (error) {
      console.error('Error fetching data for earTag:', item.earTag, error);
    }
  }
};


//增行
const c = ref(0);
const Addform = () => {
  c.value = 1;
  tableDate.forEach((item) => {
    c.value++;
  });
  tableDate.push({
    "earTag": "",
    "gestationDays": 0,
    "difficultyDelivery": 0,
    "childbirthDate": "",
    "fatSheep": 0,
    "motherHood": 0,
    "lactation": 0,
    "mastitis": 0,
    "broodNumber": "",
    "broodWeight": 0,
    "litterWeight": 0,
    "liveLambNum": 0,
    "liveLambWeight": 0,
    "liveLambAvgWeight": 0,
    "dieLambNum": 0,
    "dieLambWeight": 0,
    "dieLambAvgWeight": 0,
    "healthyNumber": 0,
    "lambNumber": 0,
    "malformationNumber": 0,
    "stillbirthNumber": 0,
    "mummyNumber": 0,
    "freaksNumber": 0,
    "liveNumber": 0,
    "sex": 0,
    "currentBuilding": 0,
    "currentField": 0,
    "toBuildingManageId": 0,
    "toColumMangementId": 0,
  })
}
//删行
const Deleteform = () => {
  tableDate.pop();
  localStorage.setItem("data", JSON.stringify(tableDate));
};

//添加方法
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      tableDate.forEach(c => {
        c.userId = form.userId,
          c.childbirthDate = form.childbirthDate
      })
      console.log(tableDate)
      sheepDeliverys.addpregnancy(tableDate).then(res => {
        ElMessage.success('添加成功')
        c.value = res

      })

    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

onMounted(() => {
  getBuildingIdList()
  getSheepPregnancy()
})


</script>
